package com.ruoyi.service.impl;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.result.Result;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.domain.*;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.mapper.T21Mapper;
import com.ruoyi.service.IT03Service;
import com.ruoyi.service.IT18Service;
import com.ruoyi.service.IT21Service;
import com.ruoyi.service.IT24Service;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;


/**
 * 企业知识库版本Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-28
 */
@Service
public class T21ServiceImpl implements IT21Service
{
    @Autowired
    private T21Mapper t21Mapper;

    @Autowired
    private IT24Service t24Service;

    @Autowired
    private IT18Service t18Service;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IT03Service t03Service;

    /**
     * 查询企业知识库版本
     *
     * @param F2101 企业知识库版本主键
     * @return 企业知识库版本
     */
    @Override
    public T21 selectT21ByF2101(Long F2101)
    {
        return t21Mapper.selectT21ByF2101(F2101);
    }

    /**
     * 查询企业知识库版本列表
     *
     * @param t21 企业知识库版本
     * @return 企业知识库版本
     */
    @Override
    public List<T21> selectT21List(T21 t21)
    {
        PageUtils.startPage();
        return t21Mapper.selectT21List(t21);
    }

    /**
     * 新增企业知识库版本
     *
     * @param t21 企业知识库版本
     * @return 结果
     */
    @Override
    public int insertT21(T21 t21)
    {
        T21 t21Now = t21Mapper.selectMaxT21ByF0301(t21.getF0301());
        if(!ObjectUtils.isEmpty(t21Now)){
            throw new RuntimeException("此客户已有版本，不允许通过此方式新增版本");
        }
        t21.setF2105(1L);
        t21.setCreateDate(new Date());
        t21.setDelFlag("0");
        List<Long> F2106 = new ArrayList<>();
        t21.setF2106(JSON.toJSONString(F2106));
        t21.setF2108(0L);
        return t21Mapper.insertT21(t21);
    }

    /**
     * 修改企业知识库版本
     *
     * @param t21 企业知识库版本
     * @return 结果
     */
    @Override
    public int updateT21(T21 t21)
    {
        return t21Mapper.updateT21(t21);
    }

    /**
     * 批量删除企业知识库版本
     *
     * @param F2101s 需要删除的企业知识库版本主键
     * @return 结果
     */
    @Override
    public int deleteT21ByF2101s(Long[] F2101s)
    {
        return t21Mapper.deleteT21ByF2101s(F2101s);
    }

    /**
     * 删除企业知识库版本信息
     *
     * @param F2101 企业知识库版本主键
     * @return 结果
     */
    @Override
    public int deleteT21ByF2101(Long F2101)
    {
        return t21Mapper.deleteT21ByF2101(F2101);
    }

    @Override
    public int selectMaxNumber(Long f0301) {
        return t21Mapper.selectMaxNumber(f0301);
    }

    @Override
    public T21 selectT21ByF2105(Long F2105) {
        return t21Mapper.selectT21ByF2105(F2105);
    }

    @Override
    @Transactional
    public AjaxResult changeVerson(HttpServletRequest request, Long f2101) {

        //获取历史版本
        T21 t21 = t21Mapper.selectT21ByF2101(f2101);

        //获取当前的使用版本
        T21 t21Now = t21Mapper.selectT21ByDelFlag("0");
        t21Now.setDelFlag("1");
        t21Mapper.updateT21(t21Now);

        t21.setDelFlag("0");
        t21Mapper.updateT21(t21);

        return AjaxResult.success();
    }

    @Override
    public T21 selectT21ByDelFlag(String delFlag) {
        return t21Mapper.selectT21ByDelFlag(delFlag);
    }

    @Override
    public T21 selectT21ByF0301AndDelFlag(Long f0301, String number) {
        return t21Mapper.selectT21ByF0301AndDelFlag(f0301,number);
    }

    @Override
    @Transactional
    public AjaxResult addActivity(T21 t21, HttpServletRequest request) {
        T24 t24 = new T24();
        Long userId=tokenService.getLoginUser(request).getUser().getUserId();
        T18 t18 = t18Service.selectT18ByF1801(userId);
        t24.setF1801(t18.getF1801());
        t24.setF2402(t18.getF1804());
        t24.setF2403(0L);
        String id = UUID.randomUUID().toString();
        t24.setF2404("审核提交");
        t24.setF2405(id);
        t24.setF2406(new Date());
        t24.setF2407(1L);
        List<Long> list = new ArrayList<>();
        for(T09 t09 : t21.getT09List()){
            list.add(t09.getF0901());
        }
        t21.setF2106(JSON.toJSONString(list));
        t21.setF2107(id);
        t21.setF2108(1L);
        t21.setF0301(t18.getF0301());
        t21.setF2102(t18.getF1808());
        t21.setUpdateDate(new Date());

        T21 maxT21 = t21Mapper.selectMaxT21ByF0301(t18.getF0301());
        t21.setF2103(maxT21.getF2103());
        t21.setF2104(maxT21.getF2104());
        t21.setF2105(maxT21.getF2105()+1);
        t21.setCreateDate(new Date());
        t21.setDelFlag("1");
        t21.setF2108(2L);
        t24Service.insertT24(t24);
        t21Mapper.insertT21(t21);
        return AjaxResult.success("添加申请成功");
    }
    @Override
    @Transactional
    public AjaxResult addActivitys(T21 t21, HttpServletRequest request, Long version) {
        T21 t21Old = t21Mapper.selectT21ByF2101(version);
        if(ObjectUtils.isEmpty(t21Old)){
            return this.addActivity(t21,request);
        }else{
            Set old = JSON.parseObject(t21Old.getF2106(), Set.class);
            Set newSet = JSON.parseObject(t21.getF2106(), Set.class);
            if(old.containsAll(newSet)&&newSet.containsAll(old)){
                Long userId=tokenService.getLoginUser(request).getUser().getUserId();
                T18 t18 = t18Service.selectT18ByF1801(userId);
                T24 t24 = new T24();
                t24.setF1801(t18.getF1801());
                t24.setF2402(t18.getF1804());
                t24.setF2403(0L);
                String id = UUID.randomUUID().toString();
                t24.setF2404("审核提交");
                t24.setF2405(id);
                t24.setF2406(new Date());
                t24.setF2407(1L);
                t24Service.insertT24(t24);
                t21Old.setF2107(id);
                t21Old.setF2108(2L);
                t21Old.setCreateDate(new Date());
                t21Old.setUpdateDate(new Date());
                t21Mapper.updateT21(t21Old);
            }else{
                return this.addActivity(t21,request);
            }
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult toProcessPass(HttpServletRequest request, Long f2101) {
        if(ObjectUtils.isEmpty(f2101)){
            return AjaxResult.error("参数错误");
        }
        Long userId=tokenService.getLoginUser(request).getUser().getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        T21 t21 = t21Mapper.selectT21ByF2101(f2101);
        T24 t24 = new T24();
        t24.setF2402(sysUser.getNickName());
        t24.setF2403(0L);
        t24.setF2404("审核");
        t24.setF2405(t21.getF2107());
        t24.setF2406(new Date());
        t24.setF2407(1L);
        t24Service.insertT24(t24);
        //变化知识库的使用。

        t21.setUpdateDate(new Date());
        T21 t21Now = t21Mapper.selectT21ByF0301AndDelFlag(t21.getF0301(), "0");

        t21.setDelFlag("0");
        t21.setF2108(0L);

        t21Mapper.updateT21(t21);
        t21Now.setDelFlag("1");
        t21Mapper.updateT21(t21Now);
        return AjaxResult.success();
    }

    @Override
    public T21 selectT21ByF2405(String f2405) {
        return new T21();
    }

    @Override
    public AjaxResult toProcessOut(HttpServletRequest request, Long f2101) {
        if(ObjectUtils.isEmpty(f2101)){
            return AjaxResult.error("参数错误");
        }
        Long userId=tokenService.getLoginUser(request).getUser().getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        T21 t21 = t21Mapper.selectT21ByF2101(f2101);
        T24 t24 = new T24();
        t24.setF2402(sysUser.getNickName());
        t24.setF2403(1L);
        t24.setF2404("审核");
        t24.setF2405(t21.getF2107());
        t24.setF2406(new Date());
        t24.setF2407(1L);
        t24Service.insertT24(t24);
        t21.setF2108(1L);
        t21Mapper.updateT21(t21);
        return AjaxResult.success();
    }

    @Override
    public T21 selectT21ByF2107(String f2107) {
        T21 t21 = t21Mapper.selectT21ByF2107(f2107);
        return t21;
    }
}
